
import { getAntd, getEcCom, getComsMobx } from '../../util/convert';
import { observer, inject } from "mobx-react/index";
import * as mobx from 'mobx';

@inject('globalStore', 'freeWflowSimpleStore')
@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
    }
    componentWillMount() {

        const { loadCondition } = this.props.freeWflowSimpleStore;
        const { baseParam = {} } = this.props.globalStore;
        setTimeout(() => {//刚加载表单的时候全量组件库还没加载，此时会报WeaForm取不到的错
            loadCondition(baseParam);
        }, 1000)
    }
    componentDidMount(){
        $('#container').bind('click', function (e) {
            jQuery(".wf-req-freeWorkflowSimple").css("border","1px solid #ddd")
        })
    }

    render() {
        return (
            <div className='wf-req-freeWorkflowSimple'>
                {this.getSearchs()}
            </div>
        )
    }
    getSearchs = () => {
        const { WeaTools, WeaFormItem, WeaSearchGroup } = getEcCom();
        const { WeaSwitch } = getComsMobx();
        const { freeWflowSimpleStore } = this.props;
        const { form, conditioninfo } = freeWflowSimpleStore;
        if (!form) return;
        const formParams = form.getFormParams();
        let group = [];
        form.isFormInit && mobx.toJS(conditioninfo).map((c,ecindex) => {
            let items = [];
            c.items.map(field => {
                let col = 1;
                const domKey = typeof field.domkey === "object" ? field.domkey[0] : field.domkey;
                if (formParams.isToNewAddSign && formParams.isToNewAddSign == 2) {
                    if (field.domkey[0] === "isToNewAddSign") {
                        items.push({
                            com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@rhwoi2@${domKey}`}
                                label={`${field.label}`}
                                labelCol={{ span: 4, offset: 4 }}
                                ratio1to5
                                error={form.getError(field)}
                                tipPosition="bottom"
                                wrapperCol={{ span: 16 }}>
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@oz5jy4@${domKey}`} fieldConfig={field} form={form} onChange={this.onChange} />
                            </WeaFormItem>),
                            col: col,
                            hide: form.isHide(field, (keys, allParams) => {
                                return false;
                            })
                        })
                    }
                } else {
                    if(field.domkey[0] == 'approvalSettings') {
                        let tmpOptions = field.options;
                        const {approvalSettings = ''} = formParams;
                        const checkOne = approvalSettings.indexOf('1') > -1;
                        const checkTwo = approvalSettings.indexOf('2') > -1;
                        tmpOptions = tmpOptions.map((item) => {
                            if(item.key == '3' || item.key == '4') {
                                item.disabled = !checkOne;
                            }
                            if(item.key == '2') {
                                item.disabled = checkOne;
                            }
                            if(item.key == '5') {
                                item.disabled = !checkTwo;
                            }
                            if(item.key == '6' || item.key == '7') {
                                item.disabled = !checkOne&&!checkTwo;
                            }
                            return item;
                        })
                        field.options = tmpOptions;
                    }
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@jcngon@${domKey}`}
                            label={`${field.label}`}
                            labelCol={{ span: 4, offset: 4 }}
                            ratio1to5
                            error={form.getError(field)}
                            tipPosition="bottom"
                            wrapperCol={{ span: (field.domkey[0] === "nodeOperator" || field.domkey[0] === "endTransfer") ? 8 : 16 }}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@7s2p43@${domKey}`} fieldConfig={field} form={form} onChange={this.onChange} />
                        </WeaFormItem>),
                        col: col,
                        hide: form.isHide(field, (keys, allParams) => {
                            return false;
                        })
                    })
                }

            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@e0bjiu@${ecindex}`} needTigger={false} title={c.title} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    onChange = (v, c) => {
        const { changeForm, form, chengeSubmitParams, setState,updateFormFields } = this.props.freeWflowSimpleStore;
        if (v["isToNewAddSign"]) {
            changeForm(c.value)
        } else {
            let formParams = form.getFormParams();
            if(formParams.isToNewAddSign==1){
                setState({toNewFormData: form.getFormDatas()})
            }
            let submitParams = {}
            if (v && v.hasOwnProperty("nodeOperator")) {
                submitParams.operatorids = v.nodeOperator.value || "";
            } else if (v && v.hasOwnProperty("countersignAttribute")) {
                submitParams.countersignAttribute = v.countersignAttribute.value || "";
            } else if (v && v.hasOwnProperty("endTransfer")) {
                submitParams.isFlowToNext = v.endTransfer.value || "";
            } else if(v && v.hasOwnProperty('approvalSettings')) {
                let val = v.approvalSettings.value||'';
                let temp_val =['1','2','3','4','5','6','7'];
                let temp_checked = {};
                temp_val.map(vkey => {
                    temp_checked[vkey] = val.indexOf(vkey) > -1;
                });

                if(temp_checked['1'] == true) {
                    temp_checked['2'] = true;
                }else {
                    temp_checked['3'] = false;
                    temp_checked['4'] = false;
                }

                if(temp_checked['2'] == false){
                    temp_checked['5'] = false;
                }

                if(temp_checked['1']== false&&temp_checked['2']==false){
                    temp_checked['6'] = false;
                    temp_checked['7'] = false;
                }
                val = '';
                temp_val.map(vkey => {
                    if(temp_checked[vkey] == true){
                        if(val == ''){
                            val = vkey;
                        }else{
                            val += `,${vkey}`;
                        }
                    }
                });
                updateFormFields({approvalSettings:val});
                submitParams.approvalSettings = val;

            }
            chengeSubmitParams(submitParams);
        }
    }

}